package love.yuanmengrobot.attendancesystem.servlet;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/6/20 16:50
 */

import love.yuanmengrobot.attendancesystem.dao.ClazzDao;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

public class ClazzServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        ciClass(request,response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
//        String name = request.getParameter("name");
//        String age = request.getParameter("age");
//        PrintWriter pw = response.getWriter();
//        pw.print("您使用POST方式请求该Servlet。<br />" + "name = " + name + ",age = " + age);
//        pw.flush();
//        pw.close();

//        request.getRequestDispatcher("myclazz.jsp").forward(request, response);
    }

    public void ciClass(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {

        try {
            request.setCharacterEncoding("utf-8");


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=utf-8");
        String memberid = request.getParameter("memberid");
        String classname = request.getParameter("classname");
        String clazzid = request.getParameter("clazzid");
        String op = request.getParameter("op");
        PrintWriter pw = null;
        try {
            pw = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ClazzDao clazzDao =new ClazzDao();
        int su=0;
        if("add".equals(op)){
            //创建班级
           su=  clazzDao.insertAdd(classname,memberid);
           if(su==1){
               pw.print("{\"meg\":\"创建成功\"}");
           }else {
               pw.print("{\"meg\":\"创建失败\"}");
           }

        }else{
            //加入班级
            su=  clazzDao.insertClazz(clazzid,memberid);
            if(su==1){
                pw.print("{\"meg\":\"加入成功\"}");
            }else {
                pw.print("{\"meg\":\"加入失败\"}");
            }

        }

//        PrintWriter pw = response.getWriter();
//        pw.print("您使用GET方式请求该Servlet。<br />" + "name = " + name + ",age = " + age);
        pw.flush();
        pw.close();
    }
}
